AddParamTypeBasedOnPHPUnitDataProviderRector: Enhance existing rule to handle PHPUnit 10+ DataProvider Attribute#4925
Conversation
| #[\PHPUnit\Framework\Attributes\DataProvider('provideData')] | ||
| public function test_with_attribute(string $name) | ||
| { | ||
| } |
There was a problem hiding this comment.
Could you separate these changes to a new fixture file? It will be easier to connect together in years ahead blame
There was a problem hiding this comment.
Hi - apologies, have just pushed up some changes.
Separated all tests into new files in the same directory as the original tests.
856cd55 to
c01370e
Compare
|
@TomasVotruba please be aware one test started failing after the push before last: https://github.com/rectorphp/rector-src/actions/runs/6282888032/job/17062803915#step:5:97 I changed the test Change: public static function provideData(): array
{
return [
- [ new class {} ],
+ [ self::SOMETHING ],
];
}
}
We cannot do similar as the existing test not using the attribute because public static providers in PHPUnit 10+ do not allow arguments passed, I believe. |
c01370e to
4f67ab1
Compare
This is so that it can be something more generic.
4f67ab1 to
c813469
Compare
This should accomodate for the `#[\PHPUnit\Framework\Attributes\DataProvider]` way of declaring data providers now, from PHPUnit 10: https://docs.phpunit.de/en/10.0/writing-tests-for-phpunit.html#data-providers
This test code was invalid when I pasted it into the online rector demo tool originally, due to a missing semi colon.
After running: `composer run docs`
c813469 to
3512af5
Compare
|
Hi @TomasVotruba - I have made some changes to accommodate for changes around this rule merged earlier today in #5068. I had to refactor my original code slightly, but I think I have now accommodated for the changes @jlherren made. This includes adding 2 more tests around use of the attribute instead and also mixing between php doc and attribute. The changes in #5068 also address some concerns I added in the description of this merge request involving multiple providers, which is nice! I believe I also made all other changes around your comments. Have requested a re-review, thanks. I am not sure on how you prefer to review in terms of marking comments as resolved, for now I have left that up to you. |
|
Looks good, thank you 👍 |
The purpose of this PR is to hopefully address rectorphp/rector#8179.
I have tried to add atomic commits which should hopefully explain the changes made if you have time to review them commit by commit. I first started by adding failing tests as part of the first commit, which when running when checked out on that commit, fail.
I then refactored the existing code slightly to reuse the existing logic but with a renamed variable name, and then made the changes which I hope are along the right lines and within the expectations of the contributing guide.
The new method
getPhpDataProviderAttribute, I added, is very similar to the existing code ofrector-src/rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php
Lines 31 to 44 in 7725801
hasAttributecheck, I would rather just loop through the attributes and collect what was needed (If the Attribute is the one we want here).I am also not sure if we have to add a check that the version of PHP should be at least 8.0 for this feature, I tried to do so but the existing configured rule logic caused my tests to fail when having the following check in the rule:
because of the existing code:
rector-src/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector/config/configured_rule.php
Line 12 in 48febdb
I have left out the above
isAtLeastPhpVersioncheck for now.Other notes
;, I can remove this if you would like it to be separate from this PR / want it gone.Technical notes
which we probably want to do here.
Should we account for this?
Please let me know if you would like to make any changes, or please feel free to make any modifications to suit your needs/code style, if preferred.
I am fairly new to writing code in Rector but it is a rather enjoyable thing to work with.